
let uid = 0

export default class Dep {
  constructor() {
    this.id = uid++
    this.subs = []
  }

  addSub(sub) {
    this.subs.push(sub)
  }

  removeSub() {
    // #12696 deps with massive amount of subscribers are extremely slow to
    // clean up in Chromium
    // to workaround this, we unset the sub for now, and clear them on
    // next scheduler flush.
    this.subs[this.subs.indexOf(sub)] = null
    if (!this._pending) {
      this._pending = true
      pendingCleanupDeps.push(this)
    }
  }
  // 收集
  depend() {
    // 我希望watcher可以存放dep
    // this.subs.push(Dep.target)
    Dep.target.addDep(this)
  }
  // 更新
  notify() {
    // stabilize the subscriber list first
    for (let i = 0; i < this.subs.length; i++) {
      const sub = this.subs[i]
      sub.update()
    }
  }
}

// The current target watcher being evaluated.
// This is globally unique because only one watcher
// can be evaluated at a time.
Dep.target = null
const stack = []
//添加
export function pushTarget(target) {
  stack.push(target)
  Dep.target = target
}
// 删除
export function popTarget() {
  stack.pop()
  // Dep.target = null
  Dep.target = stack[stack.length - 1]
}
